import { createSlice, PayloadAction } from '@reduxjs/toolkit';

/*
* ticketSettingStatus（票种设置）: create:创建 update:更新  edit:修改
* createActivityType(用于复制活动、创建活动、编辑活动) create:创建 update:更新  edit:修改
*/


interface createActiveState {
    ticketData: any[],
    ticketSettingStatus: string,
    editTicketSetting: boolean,
    editTicketIndex: number,
    activityStartTime: string,
    activityEndTime: string,
    inputData: any,
    activityTypeObj: any,
    opennessObj: any,
    editActivityId: string
    editActivityDetail: any,
    createActivityType: string,
}

const initialState: createActiveState = {
    ticketData: [],
    ticketSettingStatus: "create",
    createActivityType: "create",
    editTicketSetting: false,
    editTicketIndex: 0,
    activityStartTime: "",
    activityEndTime: "",
    inputData: {
        isManualReviewed: false,
        isWrittenOff: false,
        imgUrls: [],
        tags: [],
        durationStartTime: undefined,
        durationEndTime: undefined,
        time: undefined
    },
    activityTypeObj: {},
    opennessObj: {},
    editActivityId: "",
    editActivityDetail: {},

};

const createActiveSlice = createSlice({
    name: 'createActive',
    initialState,
    reducers: {
        setTicketData(state, action) {
            console.log('action', action);
            state.ticketData = [...state.ticketData, action.payload.ticketData];
        },
        setTicketBylist(state, action) {
            state.ticketData = action.payload.ticketData;
        },
        resetTicketData(state, action) {
            state.ticketData = [];
        },
        updateTicketData(state, action) {
            const list = []
            state.ticketData.forEach((item: any, index: number) => {
                if (state.editTicketIndex == index) {
                    const tmpItem = {
                        ...action.payload.ticketItem
                    }
                    list.push(tmpItem);
                } else {
                    list.push(item);
                };
            })
            state.ticketData = list;
            state.ticketSettingStatus = 'create';
        },
        removeTicketDataByIndex(state, action) {
            const idx = action.payload.index
            const list = [];
            state.ticketData.map((item: any, index: number) => {
                if (index !== idx) {
                    list.push(item);
                };
            })
            state.ticketData = list;
        },
        changeTicketDataIsOnSaleStatus(state, action) {
            const idx = action.payload.index
            const status = action.payload.status;
            const list = [];
            state.ticketData.map((item: any, index: number) => {
                if (index === idx) {
                    item.isOnSale = status;
                };
                list.push(item);
            })
            state.ticketData = list;
        },
        setEditTicketSetting(state, action) {
            state.editTicketSetting = action.payload.editTicketSetting;
        },
        setEditTicketIndex(state, action) {
            state.editTicketIndex = action.payload.index;
        },
        setTicketSettingStatus(state, action) {
            state.ticketSettingStatus = action.payload.ticketSettingStatus
        },
        setActivityTimeforRedux(state, action) {
            console.log('action', action)
            state.activityStartTime = action.payload.startT;
            state.activityEndTime = action.payload.endT;
        },
        setInputDataByItem(state, action) {
            console.log('action', action);
            state.inputData = {
                ...state.inputData,
                ...action.payload,
            };
        },
        setActivityTypeObj(state, action) {
            state.activityTypeObj = action.payload.activityTypeObj;
        },
        setOpennessObj(state, action) {
            state.opennessObj = action.payload.opennessObj;
        },
        resetInputData(state, action) {
            const inputData = {
                isManualReviewed: false,
                isWrittenOff: false,
                imgUrls: [],
                tags: [],
                durationStartTime: undefined,
                durationEndTime: undefined,
                time: undefined
            };
            state.ticketData = [];
            state.inputData = inputData;
            state.activityTypeObj = {};
            state.opennessObj = {};
        },
        setEditActivityId(state, action) {
            state.editActivityId = action.payload.editActivityId;
        },
        setCreateActivityType(state, action) {
            state.createActivityType = action.payload.createActivityType;
        },
    },
});

export const {
                resetTicketData,
                resetInputData,
                setOpennessObj,
                setActivityTypeObj,
                setInputDataByItem,
                setActivityTimeforRedux,
                updateTicketData,
                setTicketSettingStatus,
                setEditTicketIndex,
                setEditTicketSetting,
                setTicketData,
                removeTicketDataByIndex,
                changeTicketDataIsOnSaleStatus,
                setEditActivityId,
                setTicketBylist,
                setCreateActivityType
            } = createActiveSlice.actions;
export default createActiveSlice.reducer;
